package com.wjx.article_sys.controller;

import com.wjx.article_sys.entity.Article;
import com.wjx.article_sys.entity.Comment;
import com.wjx.article_sys.service.impl.ArticleServiceImpl;
import com.wjx.article_sys.service.impl.CommentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/front")
public class FrontController {
    @Autowired
    private ArticleServiceImpl articleService;
    @Autowired
    private CommentServiceImpl commentService;
    @Autowired
    private HttpSession session;

    @GetMapping("/article")
    public String articleDetail(@RequestParam("id") Integer id,
                                @RequestParam(name = "isBack", defaultValue = "0")
                                int isBack, Model model) {
        Article article = articleService.queryArticleById(id);
        model.addAttribute("article", article);
        if (isBack == 0) {
            List<Comment> commentList = commentService.queryCommentByArticleId(id);
            model.addAttribute("commentList", commentList);
        }
        model.addAttribute("isBack", isBack);
        return "front/articleDetail";
    }

    @PostMapping("/addComment")
    public String addComment(@RequestParam("content") String content,
                             @RequestParam("articleId") Integer articleId,
                             @RequestParam(name = "isBack", defaultValue = "0") int isBack,
                             Model model) throws Exception {
        commentService.addComment(content, articleId);
        Article article = articleService.queryArticleById(articleId);
        List<Comment> commentList = commentService.queryCommentByArticleId(articleId);
        model.addAttribute("article", article);
        model.addAttribute("commentList", commentList);
        model.addAttribute("userName", session.getAttribute("userName"));
        model.addAttribute("isBack", isBack);
        return "front/articleDetail";
    }

    @GetMapping("/toModifyComment")
    public String toModifyComment(@RequestParam("id") Integer id,
                                  @RequestParam("content") String content,
                                  @RequestParam("aId") Integer aId,
                                  Model model) {
        model.addAttribute("id", id);
        model.addAttribute("content", content);
        model.addAttribute("aId", aId);
        return "front/modifyComment";
    }

    @PostMapping("/modifyComment")
    public String modifyComment(@RequestParam("id") Integer id,
                                @RequestParam("content") String content,
                                @RequestParam(name = "isBack", defaultValue = "0") int isBack,
                                @RequestParam("aId") Integer aId, Model model) throws Exception {
        commentService.updateCommentById(id, content);
        Article article = articleService.queryArticleById(aId);
        model.addAttribute("article", article);
        List<Comment> commentList = commentService.queryCommentByArticleId(aId);
        model.addAttribute("commentList", commentList);
        model.addAttribute("isBack", isBack);
        return "front/articleDetail";
    }

    @GetMapping("/deleteComment")
    public String deleteComment(@RequestParam("id") Integer id,
                                @RequestParam(name = "isBack", defaultValue = "0") int isBack,
                                @RequestParam("aId") Integer aId, Model model) throws Exception {
        commentService.deleteCommentById(id);
        Article article = articleService.queryArticleById(aId);
        model.addAttribute("article", article);
        List<Comment> commentList = commentService.queryCommentByArticleId(aId);
        model.addAttribute("commentList", commentList);
        model.addAttribute("isBack", isBack);
        return "front/articleDetail";
    }

}
